<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    let str = "今天是2022-11-21";
    let reg = /([0-9]{4})-([0-9]{2})-([0-9]{2})/
    // 进行捕获
    let res = reg.exec(str)
    console.log(res)
    console.log('年:', res[1])
    // 出现的问题：  如果正则的结构发生改变，加入星期等，依靠索引并不能保险地取出值
    // 解决  ES9新增正则命名捕获

    let reg2 = /(?<year>[0-9]{4})-(?<month>[0-9]{2})-(?<day>[0-9]{2})/
    let res2 = reg2.exec(str)
    console.log(res2)
    let {year, month, day} = res2.groups
    console.log(year, month, day)
</script>
</body>
</html>
